//
//  PickerMoneyView.m
//  CarCare
//
//  Created by ileo on 14-8-28.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "PickerView.h"

#define Time 0.4
#define Height 220

@interface PickerView() <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, copy) NSArray *pickDatas;

@property (nonatomic, assign) NSInteger pickRow;

@end

@implementation PickerView

-(id)initWithDatas:(NSArray *)datas clickOnOK:(void (^)(NSInteger))ok clickOnCancel:(void (^)())cancel{
    self = [super initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, Height)];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.pickDatas = datas;
        self.top = __MAIN_HEIGHT_64;
        
        self.layer.masksToBounds = YES;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
        [self addSubview:view];
        view.layer.borderColor = [UIColor lightGrayColor].CGColor;
        view.layer.borderWidth = 0.5;
        
        __weak __typeof(self) wself = self;
        UIButton *cancelBut = [[UIButton buttonWithFrame:CGRectMake(0, 0, 70, 40) title:@"取消" textColor:[UIColor blackColor] font:FONT_BUTTON click:^{
            if (cancel) {
                cancel();
            }
            [wself dismissWithAnimation:YES];
        }] setupOnView:self];

         UIButton *okBut = [[UIButton buttonWithFrame:cancelBut.frame title:@"确认" textColor:[UIColor blackColor] font:FONT_BUTTON click:^{
            if (ok) {
                ok(wself.pickRow);
            }
            [wself dismissWithAnimation:YES];
        }] setupOnView:self];
        okBut.right = __MAIN_WIDTH;
        
        self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 180)];
        self.picker.bottom = self.frame.size.height;
        self.picker.backgroundColor = [UIColor whiteColor];
        self.picker.dataSource = self;
        self.picker.delegate = self;
        self.picker.showsSelectionIndicator = YES;
        [self addSubview:self.picker];
        
    }
    return self;
}

#pragma mark - 
-(void)showWithAnimaton:(BOOL)animation{
    if (self.bottom == __MAIN_HEIGHT_64) {
        return;
    }
    if (animation) {
        if (_delegate) {
            [_delegate pickerWillShowHeight:Height time:Time];
        }
        __weak __typeof(self) wself = self;
        [UIView animateWithDuration:Time animations:^{
            wself.bottom = __MAIN_HEIGHT_64;
        }];
    }else{
        if (_delegate) {
            [_delegate pickerWillShowHeight:Height time:0];
        }
        self.bottom = __MAIN_HEIGHT_64;
    }
}

-(void)dismissWithAnimation:(BOOL)animation{
    if (self.top == __MAIN_HEIGHT_64) {
        return;
    }
    if (animation) {
        if (_delegate) {
            [_delegate pickerWillDismissHeight:0 time:Time];
        }
        __weak __typeof(self) wself = self;
        [UIView animateWithDuration:Time animations:^{
            wself.top = __MAIN_HEIGHT_64;
        }];
    }else{
        if (_delegate) {
            [_delegate pickerWillDismissHeight:0 time:0];
        }
        self.top = __MAIN_HEIGHT_64;
    }
}

#pragma mark - picker

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.pickDatas.count;
}

//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return self.pickDatas[row];
//}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.pickRow = row;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *label;
    if (view.tag == 17) {
        label = (UILabel *)view;
    }else{
        label = [Factory labelWithFrame:CGRectMake(0, 0, pickerView.width, 35) font:[UIFont boldSystemFontOfSize:19] text:nil textColor:nil onView:nil textAlignment:NSTextAlignmentCenter];
        label.tag = 17;
    }
    label.text = self.pickDatas[row];
    return label;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
